package com.ejuornal.action;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.ejuornal.bean.User;
import com.ejuornal.bean.UserExt;
import com.ejuornal.service.UserExtService;
import com.ejuornal.service.UserService;
import com.ejuornal.springutil.SpringUtil;

@Component("userExtAction")
@Scope("prototype")
public class UserExtAction extends BaseAction<UserExt> {
	private static final long serialVersionUID = 1L;
	private UserExtService userExtService = null;
	private UserExt userExt;
	private ArrayList<String> selected;
	
	private String userExtIdString=null;
	private File upload;//上传文件
	private String uploadFileName;//上传文件名
	private String uploadContentType;
	
	private UserService userService;
    private User user;
    
    private String value;//定义变量-判断是哪个页面请求的信息--根据变量返回正确地视图
//	添加会员扩展信息
	public String addExt()
	{ 
		String realPath = ServletActionContext.getServletContext().getRealPath("/attached/images");
		String dbPicSrc = null;//存储路径

		if(upload != null){
			File saveDir = new File(realPath);
			//图片重命名
			String ext = uploadFileName.split("\\.")[1];//后缀名
			Calendar cal = Calendar.getInstance();
			String reName = String.valueOf(cal.getTimeInMillis());
			dbPicSrc = "attached/images/"+reName+"."+ext;
			uploadFileName = reName+"."+ext;
			
			if(saveDir.exists()){
				File saveFile = new File(saveDir, uploadFileName);
				try {
					FileUtils.copyFile(upload, saveFile);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			else{
				saveDir.mkdirs();
				File saveFile = new File(saveDir, uploadFileName);
				try {
					FileUtils.copyFile(upload, saveFile);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	  userExt.setHeadPic(dbPicSrc);
	  userExt.setUserId(super.getUserIdBySession());
	  userExtService.add(userExt);
	  return  "addExt";	
		
	}
//	显示扩展详细信息
	public String detailExt()
	{

		int userId=super.getUserIdBySession();
		userService=(UserService) SpringUtil.getBean("userService");
		user=userService.findById(userId);
		userExtService=(UserExtService) SpringUtil.getBean("userExtService");
		userExt=userExtService.findByUserId(userId);
		
		//处理异常
		if(userExt.getHobby() == null || "".equals(userExt.getHobby())) {
			return "success";
		}
		
		String[] hobbies = userExt.getHobby().split(",");
		selected = new ArrayList<String>();
		for (String string : hobbies) {
			selected.add(string.trim());
		}

	    return SUCCESS;
	}
//	查找个人资料
	public String findExt() {
		int userId=super.getUserIdBySession();
		user=userService.findById(userId);
		userExt=userExtService.findByUserId(userId);
	    return SUCCESS;
	}
	
//	会员扩展信息更改
	public String  completeExtMsg()
	{
		String realPath = ServletActionContext.getServletContext().getRealPath("/attached/images");
		String dbPicSrc = null;//存储路径
		if(upload != null){
			File saveDir = new File(realPath);
			//图片重命名
			String ext = uploadFileName.split("\\.")[1];//后缀名
			Calendar cal = Calendar.getInstance();
			String reName = String.valueOf(cal.getTimeInMillis());
			dbPicSrc = "attached/images/"+reName+"."+ext;
			uploadFileName = reName+"."+ext;
			
			if(saveDir.exists()){
				File saveFile = new File(saveDir, uploadFileName);
				try {
					FileUtils.copyFile(upload, saveFile);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			else{
				saveDir.mkdirs();
				File saveFile = new File(saveDir, uploadFileName);
				try {
					FileUtils.copyFile(upload, saveFile);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		} 
		userExt.setHeadPic(dbPicSrc);
		if(upload == null) {
		    userExt.setHeadPic(userExt.getHeadPic());
		}
		userExt.setUserExtId(Integer.parseInt(userExtIdString));
		userExtService.update(userExt);
		return SUCCESS;
	}
	/**
	 * Description 根据会员名查询会员信息
	 * @author lwf
	 * @return User
	 */
	public String findByName(){
		String viewStr=null;//根据接受的value值不同返回不同的页面
		String userName = user.getUserName();
		userService = (UserService) SpringUtil.getBean("userService");
		userExtService=(UserExtService) SpringUtil.getBean("userExtService");
		user = userService.findByUserName(userName);
		if(user != null){
			int userId = user.getUserId();
			userExt = userExtService.findByUserId(userId);
		}
		if(value.equals("addEditor")){//编辑管理
			viewStr = "editorSuccess";
		}else if(value.equals("addEditorCommit"))//编委管理
		{
			viewStr = "success";
		}
		return viewStr;
	}
	/**
	 * 通过json查找用户头像
	 * @return
	 */
	public String findById() {
		int userId = getUserIdBySession();
		if(userId == -1) {
			return ERROR;
		}
	
		userExt = userExtService.findByUserId(userId);
		dataMap.clear();
		dataMap.put("headPic", userExt.getHeadPic());
		return SUCCESS;
	}
	
	
	public UserExt getUserExt() {
		return userExt;
	}

	public void setUserExt(UserExt userExt) {
		this.userExt = userExt;
	}

	public UserExtService getUserExtService() {
		return userExtService;
	}

	@Resource(name="userExtService")
	public void setUserExtService(UserExtService userExtService) {
		this.userExtService = userExtService;
	}
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public String getUploadContentType() {
		return uploadContentType;
	}
	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	public String getUserExtIdString() {
		return userExtIdString;
	}
	public void setUserExtIdString(String userExtIdString) {
		this.userExtIdString = userExtIdString;
	}
   public UserService getUserService() {
        return userService;
    }
    @Resource(name="userService")
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}

	public ArrayList<String> getSelected() {
		return selected;
	}
	public void setSelected(ArrayList<String> selected) {
		this.selected = selected;
	}
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}
	
}
